<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <div class="progress-bar-inner">
      <div class="progress-bar-inner-line" ref="progressLine"></div>
      <div class="progress-bar-inner-wrap" ref="progressBtn"
          @touchstart.prevent="progressTouchStart"
          @touchmove.prevent="progressTouchMove"
          @touchend="progressTouchEnd">
        <div class="progress-bar-inner-wrap-btn" ref="progressBtn"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      progressBtnWidth: ''
    }
  },
  created() {
    this.touch = {} 
  },
  methods: {
    /* 点击整个进度条时 */
    progressClick(e) {
      // 获取元素progressBar的大小及其相对于视口的位置（边界集合）
      const rect = this.$refs.progressBar.getBoundingClientRect()
      // 获得偏移宽度
      const offsetWidth = e.pageX - rect.left
      /* 得知播放过的百分比时操作ui变化，调整、修正位置 */
      this._offset(offsetWidth)
      this._triggerPercent()
    },
    _offset(offsetWidth) {
      // 更改progress-line的宽度
      this.$refs.progressLine.style.width = `${offsetWidth}px`
      // 移动progress-btn,即平移progress-line的宽度的位置
      this.$refs.progressBtn.style.transform = `translate3d(${offsetWidth}px,0,0)`
    },
    _triggerPercent() {
      const barWidth = this.$refs.progressBar.clientWidth - this.progressBtnWidth
      const percent = this.$refs.progressLine.clientWidth / barWidth
      this.$emit("percentChange", percent)
    },
    progressTouchStart(e) {
      // 记录已点击
      this.touch.initiated = true
      // 记录横向坐标
      this.touch.startX = e.touches[0].pageX
      // 记录进度条长度
      this.touch.left = this.$refs.progressLine.clientWidth
    },
    progressTouchMove(e) {
      if (!this.touch.initiated) {
        return
      }
      // 当前横向坐标-初始点击横向坐标
      const deltaX = e.touches[0].pageX - this.touch.startX
      const offsetWidth = Math.min(
        this.$refs.progressBar.clientWidth - this.progressBtnWidth,
        Math.max(0, this.touch.left + deltaX)
      )
      this._offset(offsetWidth)
    },
    progressTouchEnd() {
      this.touch.initiated = false
      this._triggerPercent()
    }
  },
  watch: {
    percent(newPercent) {
      if (newPercent >= 0 && !this.touch.initiated) {
        // 按钮宽度
        this.progressBtnWidth = this.$refs.progressBtn.clientWidth / 2
        // 总宽-按钮宽度
        const barWidth = this.$refs.progressBar.clientWidth - this.progressBtnWidth
        const offsetWidth = newPercent * barWidth
        this._offset(offsetWidth)
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import "@/assets/css/mixin.scss";
.progress-bar {
  width: 100%;
  height: rem(30);
}
.progress-bar .progress-bar-inner {
  height: rem(4);
  background: rgb(0, 0, 0);
  position: relative;
  top: rem(13);
}
.progress-bar-inner .progress-bar-inner-line {
  height: 100%;
  background: rgb(255, 205, 50);
  position: absolute;
}
.progress-bar-inner .progress-bar-inner-wrap {
  width: rem(30);
  height: rem(30);
  position: absolute;
  left: rem(-8);
  top: rem(-13);
}
.progress-bar-inner-wrap .progress-bar-inner-wrap-btn {
  width: rem(15);
  height: rem(15);
  background: rgb(255, 205, 50);
  box-sizing: border-box;
  border-radius: 50%;
  position: relative;
  top: rem(7);
  left: rem(7);
}
</style>
